from flask import session
from flask_restful import Resource, reqparse

from app.models import User, db, Collection

parse = reqparse.RequestParser()
parse.add_argument('u_name', type=str, help='请输入手机号')
parse.add_argument('u_password', type=str, help='请输入密码')


class RegisterResource(Resource):
    def get(self):
        pass

    def post(self):
        args = parse.parse_args()
        name = args.get('u_name')
        password = args.get('u_password')

        users = User.query.filter(User.u_name == name).all()
        if len(users) > 0:
            return {'msg': 'user already exist', 'status': 404}
        user = User()
        user.u_name = name
        user.u_password = password

        collection = Collection()
        collection.c_id = user.u_id

        db.session.add(user)
        db.session.add(collection)
        db.session.commit()
        return {'msg': 'success', 'status': 200}

    def put(self):
        pass

    def delete(self):
        pass



